package com.scu.controller;

import com.scu.pojo.Message;
import com.scu.pojo.MessageSearch;
import com.scu.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@Controller
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping(value="/checkMessage")
    @ResponseBody
    public HashMap checkMessage(HttpServletRequest request,MessageSearch info) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if (request.getSession().getAttribute("U_id") != null) {
            int u_id = (int)request.getSession().getAttribute("U_id");
            info.setId1(u_id);
            List<Message> messages = messageService.checkMessage(info);
            messageService.markReadMessage(u_id);
            int code = messages.size();
            if(code > 0) {
                resultMap.put("code", 200);
                resultMap.put("messages", messages);
            }
            else{
                resultMap.put("code", 0);
            }
        } else {
            resultMap.put("code", -1);

        }
        return resultMap;
    }

    @RequestMapping(value="/checkMessageByPage")
    @ResponseBody
    public HashMap checkMessageByPage(HttpServletRequest request,MessageSearch info){
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") != null){
            int id1 = (int)request.getSession().getAttribute("U_id");
            int id2 = Integer.parseInt(request.getParameter("target_id"));
            info.setId1(id1);
            info.setId1(id2);
            List<Message> userList = messageService.checkMessageByPage(info);
            int count = messageService.getMessageCount(info);
            resultMap.put("code",0);
            resultMap.put("count",count);
            resultMap.put("friends",userList);
            return resultMap;
        }
        else{
            resultMap.put("code",-1);
            return resultMap;
        }
    }


    @RequestMapping(value="/deleteMessage")
    @ResponseBody
    public HashMap deleteMessage(HttpServletRequest request) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") == null){
            resultMap.put("code",-1);
        }
        else{
            int u_id = (int)request.getSession().getAttribute("U_id");
            int m_id = Integer.parseInt(request.getParameter("message_id"));
            int code = messageService.deleteMessage(u_id,m_id);
            if(code > 0) {
                resultMap.put("code", 2200);
            }
            else {
                resultMap.put("code", 0);
            }
        }

        return resultMap;
    }

    @RequestMapping(value="/responseMessage")
    @ResponseBody
    public HashMap responseMessage(HttpServletRequest request) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") == null){
            resultMap.put("code",-1);
        }
        else if(request.getParameter("content") == null || request.getParameter("content").equals("") || request.getParameter("receiver_id") == null){
            resultMap.put("code",0);
        }
        else{
            int m_writer_id = (int)request.getSession().getAttribute("U_id");
            int m_receiver_id = Integer.parseInt(request.getParameter("receiver_id"));
            String m_content = request.getParameter("content");
            int code = messageService.responseMessage(m_writer_id,m_receiver_id,m_content);
            if(code > 0) {
                resultMap.put("code", 200);
            }
            else{
                resultMap.put("code", 0);
            }
        }
        return resultMap;
    }


    @RequestMapping(value="/checkUndeliveredMessage")
    @ResponseBody
    public HashMap checkUndeliveredMessage(HttpServletRequest request) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") == null){
            resultMap.put("code",-1);
        }
        else{
            int u_id = (int)request.getSession().getAttribute("U_id");
            List<Message> messages = messageService.checkUndeliveredMessage(u_id);
            int code = messages.size();
            if(code > 0) {
                resultMap.put("code", 200);
                resultMap.put("message",messages);
            }
            else{
                resultMap.put("code", -1);
            }

        }

        return resultMap;
    }


    @RequestMapping(value="/checkUndeliveredMessageByPage")
    @ResponseBody
    public HashMap checkUndeliveredMessageByPage(HttpServletRequest request,MessageSearch info){
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") != null){
            int id1 = (int)request.getSession().getAttribute("U_id");
            info.setId1(id1);
            List<Message> userList = messageService.checkUndeliveredMessageByPage(info);
            int count = messageService.getMessageCount(info);
            resultMap.put("code",0);
            resultMap.put("count",count);
            resultMap.put("friends",userList);
            return resultMap;
        }
        else{
            resultMap.put("code",-1);
            return resultMap;
        }
    }

    @RequestMapping(value="/sentUndeliveredMessage")
    @ResponseBody
    public HashMap sentUndeliveredMessage(HttpServletRequest request) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("U_id") == null){
            resultMap.put("code",-1);
        }
        else if(request.getParameter("m_id") == null){
            resultMap.put("code",0);

        }
        else{
            int m_id = Integer.parseInt(request.getParameter("m_id"));
            int u_id = (int)request.getSession().getAttribute("U_id");
            int code = messageService.sentUndeliveredMessage(u_id,m_id);
            if(code > 0) {
                resultMap.put("code", 200);
            }
            else{
                resultMap.put("code", 0);
            }
        }
        return resultMap;
    }

}
